import { qiniuGetToken } from '@/api/api'
import * as qiniu from 'qiniu-js'
// const qiniuConfig = {
//   accessKey: 'NppjS9NHakezcuPyheCzM3spTe9dtZkyjNMM1zXI',
//   secretKey: 'BmP_n1Xj8HrBJzeNaM5cbhyhzkLVFpinOL5oGp6p',
//   bucket: 'idiotic'
// }
export function fileUpload(file: File) {
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async (resolve, reject) => {
    const token = await qiniuGetToken()
    const observer = {
      next(res: object) {
        console.log('next')
        console.log(res)
      },
      error(err: object) {
        console.log('next')
        console.log(err)
        reject(err)
      },
      complete(res: object) {
        console.log('next')
        console.log(res)
        resolve(res)
      }
    }
    const putExtra = {
      fname: file.name
    }
    const config = {
      useCdnDomain: true
    }

    const options = {
      quality: 0.8,
      noCompressIfLarger: true
    }
    qiniu.compressImage(file, options).then((data: any) => {
      const observable = qiniu.upload(data.dist, file.name, token.data, putExtra, config)
      observable.subscribe(observer) // 上传开始
    })
  })
}
